const { User, Organization, Member, Sequelize } = require('../model/dbModel/index')

/**
 * 创建组织
 * @param name
 * @param description
 * @param username
 * @return {Promise<{msg: string, status: number}>}
 */
async function createOrganization(name, description, username) {
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    if (user.userrole !== "teacher") return {
        status: 12,
        msg: "对不起该用户无权限创建组织"
    }
    let org = await Organization.create({
        org_name: name,
        org_description: description,
        creator_id: user.id
    })
    console.log(org)
    if (!org) return {
        status: 50,
        msg: "服务器发生了未知错误"
    }
    return {
        status: 0,
        msg: "组织创建成功",
    }
}

/**
 * 删除组织
 * @param org_id
 * @param username
 * @return {Promise<{msg: string, deleteResult: *, status: number}|{msg: string, status: number}>}
 */
async function deleteOrganization(org_id, username) {
    return {
        msg: "接口：删除组织 联通成功"
    }
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    if (user.userrole !== "teacher") return {
        status: 12,
        msg: "对不起该用户无权限删除组织"
    }
    let org = await Organization.findOne({
        where: {
            org_id: org_id,
            creator_id: user.id
        }
    });
    if (!org) return {
        status: 12,
        msg: "用户权限不足或是该组织不存在"
    }
    let deleteResult = await Organization.destroy({
        where: {
            org_id: org_id,
        }
    });
    return {
        status: 0,
        msg: "组织" + org.org_name + "删除成功",
        deleteResult
    }
}

/**
 * 查看我拥有的组织列表
 * @param page
 * @param pageSize
 * @param username
 * @return {Promise<void>}
 */
async function organizationList(page, pageSize, username) {
    if (!page || page < 1 || !pageSize || pageSize < 1) return {
        status: 2,
        msg: "请输入正确的页码和页面大小"
    }
    let offset = (page - 1) * pageSize;
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }

    let orgs = await Organization.findAll({
        raw: true,
        offset: offset,
        limit: pageSize * 1, //隐式转换
        where: {
            creator_id: user.id
        },
    })
    let total = await Organization.count({
        raw: true,
        offset: offset,
        limit: pageSize * 1, //隐式转换
        where: {
            creator_id: user.id
        },
    })
    return {
        status: 0,
        data: {
            orgs: orgs,
            total: total,
        }
    }

}

/**
 * 用户加入组织的申请列表
 * @param org_id
 * @param username
 * @return {Promise<void>}
 */
async function applicationList(page, pageSize, org_id, username) {
    return "接口：用户加入组织的申请列表 联通成功";
}

/**
 * 同意用户们的加入组织申请
 * @param member_ids
 * @param org_id
 * @param username
 * @return {Promise<{msg: string, status: number}>}
 */
async function agreeJoins(member_ids, org_id, username) {
    return "接口：同意用户们的加入组织申请 联通成功";
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    let org = await Organization.findOne({
        where: {
            org_id: org_id,
            creator_id: user.id
        }
    });
    if (!org) return {
        status: 12,
        msg: "用户权限不足或是该组织不存在"
    }
}

/**
 * 拒绝用户们的加入组织申请
 * @param member_ids
 * @param org_id
 * @param username
 * @return {Promise<{msg: string, status: number}>}
 */
async function refuseJoins(member_ids, org_id, username) {
    return "接口：拒绝用户们加入组织申请 联通成功";
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    let org = await Organization.findOne({
        where: {
            org_id: org_id,
            creator_id: user.id
        }
    });
    if (!org) return {
        status: 12,
        msg: "用户权限不足或是该组织不存在"
    }
}

/**
 * 返回所有角色都能够查看的组织列表
 * @param page
 * @param {number} pageSize
 */
async function allOrganizationList(page,pageSize){
    if (!page || page < 1 || !pageSize || pageSize < 1) return {
        status: 2,
        msg: "请输入正确的页码和页面大小"
    }
    let offset = (page - 1) * pageSize;

    let total = await Organization.count({
        raw: true,
        offset: offset,
        limit: pageSize * 1, //隐式转换
    })
    let orgs = await Organization.findAll({
        raw: true,
        offset: offset,
        limit: pageSize * 1, //隐式转换
    })
    return {
        status: 0,
        data: {
            orgs: orgs,
            total: total,
        }
    }
}

/**
 * 查看用户所加入的组织列表
 * @param username
 * @return {Promise<{data: Model[], status: number}>}
 */
async function joinedOrganizationList(page, pageSize, username) {
    if (!page || page < 1 || !pageSize || pageSize < 1) return {
        status: 2,
        msg: "请输入正确的页码和页面大小"
    }
    let offset = (page - 1) * pageSize;
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    let orgs = await Organization.findAll({
        include: {
            model: User,
            where: {
                id: user.id,
            },
            attributes: [],
            through: {
                model: Member,
                attributes: []
            }
        }
    })
    return {
        status: 0,
        data: orgs
    }
}

/**
 * 用户加入组织
 * @param org_id
 * @param username
 * @return {Promise<{msg: string, status: number}>}
 */
async function joinOrganization(org_id, username) {
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    let org = await Organization.findOne({
        where: {
            id: org_id
        }
    });
    if (!org) return {
        status: 12,
        msg: "用户权限不足或是该组织不存在"
    }
    let member = await Member.findOne({
            where: {
                user_id: user.id,
                org_id: org.id
            }
        }).catch(err => console.log("111111", err))
        //若组织不存在该成员
    if (!member) {
        member = Member.create({
            user_id: user.id,
            org_id: org.id,
            status: 0 //这里应该是1：审核中才对，但为了赶开发进度就先置于零
        })
        return {
            status: 0,
            msg: "加入成功"
        }
    }
    //若用户已经是该组织的成员
    if (member.status === 0) {
        return {
            status: 1,
            msg: "你已经加入了该组织"
        }
    }
    //若用户的申请还在审核中
    else if (member.status === 1) {
        return {
            status: 1,
            msg: "正在审核中"
        }
    }
    //若用户的申请被拒绝
    else if (member.status === 2) {
        return {
            status: 1,
            msg: "你的申请已被拒绝"
        }
    }



}

/**
 * 用户退出组织
 * @param org_id
 * @param username
 * @return {Promise<{msg: string, status: number}>}
 */
async function quitOrganization(org_id, username) {

    return "接口：用户退出组织 联通成功";
    let user = await User.findOne({
        where: {
            username: username
        }
    })
    if (!user) return {
        status: 11,
        msg: "用户不存在"
    }
    let org = await Organization.findOne({
        where: {
            org_id: org_id,
            creator_id: user.id
        }
    });
    if (!org) return {
        status: 12,
        msg: "用户权限不足或是该组织不存在"
    }
}

module.exports = {
    createOrganization,
    deleteOrganization,
    organizationList,
    applicationList,
    agreeJoins,
    refuseJoins,
    allOrganizationList,
    joinedOrganizationList,
    joinOrganization,
    quitOrganization,
}